package com.gaswell.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaswell.common.log.LogAnnotation;
import com.gaswell.entity.Qba01;
import com.gaswell.entity.Qba01M;
import com.gaswell.entity.Qfc01;
import com.gaswell.pojo.ALLQj;
import com.gaswell.pojo.KSYmCurve;
import com.gaswell.pojo.ReciveCycleData;
import com.gaswell.service.*;
import com.gaswell.utils.DateUtils;
import com.gaswell.utils.ObjectUtils;
import com.gaswell.utils.StatusDetect;
import com.gaswell.vo.KSYmCurveVo;
import com.gaswell.vo.Qfc01Vo;
import com.gaswell.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @ClassName KSYmCurveController
 * @Author xiaow
 * @DATE 2023/2/21 15:32
 **/


@RestController
@RequestMapping("/ksymcurve")
@Api(tags = "ksymcurve 前端控制器   mysql数据库")
public class KSYmCurveController {

    @Autowired
    KSYmCurveService ksYmCurveService;

    @Autowired
    ALLQjService allQjService;

    @GetMapping("/getlatest")
    @ApiOperation(value = "获取最新曲线数据")
    @LogAnnotation(module = "ksymcurve", operator = "获取最新曲线数据")
    public Result getlatest(String userName, String jh) {
        LambdaQueryWrapper<KSYmCurve> ksYmCurveLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (jh != null && jh != "") {
            LinkedList<String> strings = new LinkedList<>();
            strings.add(jh);
            List<ALLQj> zwjhAndYwjh = allQjService.findZwjhAndYwjh(strings);
            if (zwjhAndYwjh.size() > 0)
                ksYmCurveLambdaQueryWrapper.eq(KSYmCurve::getWellName, zwjhAndYwjh.get(0).getYwbh())
                        .or()
                        .eq(KSYmCurve::getWellName, zwjhAndYwjh.get(0).getZwbh());
            else
                return Result.fail(400, "井号不存在");
        } else {
            return Result.fail(400, "井号为空");
        }
        ksYmCurveLambdaQueryWrapper.orderByDesc(KSYmCurve::getTestTime);
        ksYmCurveLambdaQueryWrapper.last("limit 1");
        List<KSYmCurve> list = ksYmCurveService.list(ksYmCurveLambdaQueryWrapper);
        LinkedList<Integer> A_list = new LinkedList<>();
        LinkedList<Integer> B_list = new LinkedList<>();
        if (list.size() > 0) {
            byte[] originalData = list.get(0).getOriginalData();
            for (int i = 0; i < originalData.length; i++) {
                if (i % 2 == 0)
                    A_list.add((int) originalData[i]);
                else
                    B_list.add((int) originalData[i]);
            }
            KSYmCurveVo ksYmCurveVo = new KSYmCurveVo();
            ksYmCurveVo.setA_list(A_list)
                    .setB_list(B_list)
                    .setTestTime(list.get(0).getTestTime())
                    .setWellName(list.get(0).getWellName())
                    .setFilterData(list.get(0).getFilterData())
                    .setOriginalData(list.get(0).getOriginalData());
            return Result.success(ksYmCurveVo);
        } else
            return Result.success(null);
    }


    @GetMapping("/gethistory")
    @ApiOperation(value = "查询历史曲线数据   时间格式  yyyy-MM-dd HH:mm:ss")
    @LogAnnotation(module = "ksymcurve", operator = "获取最新曲线数据")
    public Result getHistory(String userName, String jh, String date) {
        Timestamp timestamp = DateUtils.stringToDate(date);
        LambdaQueryWrapper<KSYmCurve> ksYmCurveLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (jh != null && jh != "") {
            LinkedList<String> strings = new LinkedList<>();
            strings.add(jh);
            List<ALLQj> zwjhAndYwjh = allQjService.findZwjhAndYwjh(strings);
            if (zwjhAndYwjh.size() > 0)
                ksYmCurveLambdaQueryWrapper.eq(KSYmCurve::getWellName, zwjhAndYwjh.get(0).getYwbh())
                        .or()
                        .eq(KSYmCurve::getWellName, zwjhAndYwjh.get(0).getZwbh());
            else
                return Result.fail(400, "井号不存在");
        } else {
            return Result.fail(400, "井号为空");
        }
        ksYmCurveLambdaQueryWrapper.eq(KSYmCurve::getTestTime, timestamp);
        List<KSYmCurve> list = ksYmCurveService.list(ksYmCurveLambdaQueryWrapper);
        LinkedList<Integer> A_list = new LinkedList<>();
        LinkedList<Integer> B_list = new LinkedList<>();
        if (list.size() > 0) {
            byte[] originalData = list.get(0).getOriginalData();
            for (int i = 0; i < originalData.length; i++) {
                if (i % 2 == 0)
                    A_list.add((int) originalData[i]);
                else
                    B_list.add((int) originalData[i]);
            }
            KSYmCurveVo ksYmCurveVo = new KSYmCurveVo();
            ksYmCurveVo.setA_list(A_list)
                    .setB_list(B_list)
                    .setTestTime(list.get(0).getTestTime())
                    .setWellName(list.get(0).getWellName())
                    .setFilterData(list.get(0).getFilterData())
                    .setOriginalData(list.get(0).getOriginalData());
            return Result.success(ksYmCurveVo);
        } else
            return Result.success(null);
    }


    @PostMapping("/saveCurve")
    @ApiOperation(value = "存储曲线数据")
    @LogAnnotation(module = "ksymcurve", operator = "存储曲线数据")
    public Result saveCurve(String userName, String jh, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        ksYmCurveService.save(new KSYmCurve()
                .setWellName(jh)
                .setTestTime(new Timestamp(System.currentTimeMillis()))
                .setOriginalData(file.getBytes()));
        return Result.success("success");
    }

}
